package com.zkingsoft.actions.admin;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.model.Paymentinfo;
import com.zkingsoft.model.Studentinfo;
import com.zkingsoft.model.TuiTuition;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.serviceUtil.ServiceUtil;
import com.zkingsoft.services.PaymentinfoService;
import com.zkingsoft.services.StudentinfoService;
import com.zkingsoft.services.TuiTuitionService;

/**
 * 退学费
 * @date 2017-04-20 20:47
 */
@Controller
@RequestMapping(value = "admin/tuiTuition")
public class TuiTuitionController extends BaseController{

	@Resource
	private TuiTuitionService currentService;
	
	@Resource
	private StudentinfoService studentInfoService;
	
	@Resource
	private ServiceUtil serviceUtil;
	
	@Resource
	private PaymentinfoService paymentinfoService;
	
	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(TuiTuition tuiTuition, PaginationVO pageVo) {
		return showList(currentService, tuiTuition, pageVo);
	}
	
	/**
	 * 查询对象
	 * */
	@RequestMapping(value = "/findModel")
	public @ResponseBody AjaxResult findModel(TuiTuition tuiTuition){
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null,currentService.findByModel(tuiTuition),0);
		return result;
		
	}
	/**
	 * 新增或者修改页面
	 */   
	@RemoveRequestToken	
   	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(TuiTuition tuiTuition) {
		List<TuiTuition> tPan = currentService.findByModel(null);
		Paymentinfo paymentinfo = paymentinfoService.findBysId(tuiTuition.getsId());
		//判断该学生是否有交学费
		if(paymentinfo==null){
			throw new GlobleException("该学生没有交学费");
		}
		//判断退费金额是否超过已交学费，如果超过则提示
		if(tuiTuition.getTuiMoney()>paymentinfo.gettInTuition()){
			throw new GlobleException("退费金额不能超过已交学费");
		}
		if (tuiTuition.getTuiId() != null) {
			
			return modify(currentService, tuiTuition, "     退学费");
		} else{
			//判断学生是否存在,如果存在则给出提示
			for(TuiTuition t : tPan){
				if(tuiTuition.getsId()== t.getsId()){
					throw new GlobleException("该学生已存在");
				}
			}
			return add(currentService, tuiTuition, "      退学费");
		}
	}
	
   	/**
	 * 进入修改界面
	 */   
	@SaveRequestToken
   	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		TuiTuition tuiTuition;
		if (id != null) {
			tuiTuition = currentService.findById(id);
			WebUtil.getRequest().setAttribute("obj", tuiTuition);
		}
		return "admin/tuiTuition-form";
	}
   	
   	
   	/**
	 * 删除
	 */  
 	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}
 	
 	@RequestMapping(value = "/queryStuByClass")
	public @ResponseBody AjaxResult queryStuByClass(Studentinfo studentinfo) {
		List<Studentinfo> stuList = studentInfoService.findByModel(studentinfo);
		if (stuList != null && stuList.size() > 0) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, "成功", stuList, stuList.size());
		}
		return new AjaxResult(AjaxResult.STATUS_ERR, null, "该班级没有学生");
	}
}